<script setup>
import { ref } from 'vue'
const count = ref(0)
// 表单数据
const formData = ref({
  name: '',
  email: ''
})
// 提交表单的处理函数
const handleSubmit = async (e) => {
  e.preventDefault(); // 防止页面刷新
  // 打印数据，查看提交前的值
  console.log(formData.value)

  try {
    const response = await fetch('http://127.0.0.1:8080/api/submitForm', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(formData.value), // 转换成JSON格式发送
    })

    if (!response.ok) {
      throw new Error('请求失败');
    }

    const result = await response.json();
    console.log(result); // 响应数据处理

    // 在成功提交后，可以清空表单或给出提示
    alert('登录成功'); // 添加提示框
    // 跳转到AI对话界面
    window.location.href = '/ai';
  } catch (error) {
     window.location.href = '/ai';
  }
}
</script>

<template>
  <div class="container">
    <h1 class="main-title">登录</h1>
    
    <!-- POST 表单 -->
    <form @submit="handleSubmit" class="form-container">
      <div class="form-group">
        <label for="name">Name:</label>
        <input type="text" id="name" v-model="formData.name" class="form-input" />
      </div>
      <div class="form-group">
        <label for="email">Email:</label>
        <input type="text" id="email" v-model="formData.email" class="form-input" />
      </div>
      <button type="submit" class="submit-btn">Submit</button>
    </form>
    <a href=http://localhost:5173/regist>链接</a>
  </div>
</template>

<style scoped>
/* 设置全局灰色背景 */
.container {
  background-color: #f5f5f5;
  min-height: 100vh;
  padding: 2rem;
  font-family: Arial, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* 主标题样式 */
.main-title {
  color: #333;
  font-size: 2.5rem;
  margin-bottom: 2rem;
  text-align: center;
}

/* 表单容器 */
.form-container {
  background-color: white;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  max-width: 600px;
  width: 100%;
  margin-top: 2rem;
}

/* 表单组样式 */
.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: bold;
  color: #333;
}

/* 输入框样式 */
.form-input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #42b983;
  box-shadow: 0 0 0 2px rgba(66, 185, 131, 0.2);
}

/* 提交按钮样式 */
.submit-btn {
  background-color: #42b983;
  color: white;
  border: none;
  padding: 0.75rem 1.5rem;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.2s;
  width: 100%;
}

.submit-btn:hover {
  background-color: #369f6d;
}
</style>